package cn.manjuu.touhou.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.activity.fragment.CardInfoFragment;
import cn.manjuu.touhou.domain.Card;
import cn.manjuu.touhou.engine.CardEngine;
import cn.manjuu.touhou.interfaces.ShowCardInfoListener;
import cn.manjuu.touhou.util.Looger;

public class CardInfoAdapter extends BaseAdapter {

	private Context mContext;
	private ShowCardInfoListener mListener;
	private List<Long> mAllCardId;
	private long mCardMaxId;

	public CardInfoAdapter(ShowCardInfoListener listener, Context mContext) {
		super();
		this.mContext = mContext;
		this.mListener = listener;
		init();
	}

	private void init() {
		mAllCardId = CardEngine.getInstance().getAllCardId();
		mCardMaxId = CardEngine.getInstance().getCardMaxId();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		 return (int) (mCardMaxId / 4 + 1);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		ViewHolder holder = null;
		if (null != convertView) {
			view = convertView;
			holder = (ViewHolder) view.getTag();
		} else {
			view = View.inflate(mContext, R.layout.card_info_item, null);
			holder = new ViewHolder();
			holder.iv_card_info_item[0] = (ImageView) view
					.findViewById(R.id.iv_card_info_item1);
			holder.iv_card_info_item[1] = (ImageView) view
					.findViewById(R.id.iv_card_info_item2);
			holder.iv_card_info_item[2] = (ImageView) view
					.findViewById(R.id.iv_card_info_item3);
			holder.iv_card_info_item[3] = (ImageView) view
					.findViewById(R.id.iv_card_info_item4);
			view.setTag(holder);
		}
		int index = 0;
		Looger.i(APP.CardInfoAdapter, "position: " + position);
		for (int i = 0; i < 4; i++) {
			index = 4 * position + i;
			if (index > mCardMaxId || !mAllCardId.contains((long) index)) {
				holder.iv_card_info_item[i].setImageBitmap(null);
				holder.iv_card_info_item[i].setOnClickListener(null);
				continue;
			}
			Looger.i(APP.CardInfoAdapter, "index: " + index);
			holder.iv_card_info_item[i]
					.setImageResource(R.drawable.pair_card_00 + index);
			holder.iv_card_info_item[i]
					.setOnClickListener(new CardOnClickListener(holder, index));
		}

		return view;
	}

	private static final class ViewHolder {

		private ImageView[] iv_card_info_item;
		private ImageView iv_card_info_item1;
		private ImageView iv_card_info_item2;
		private ImageView iv_card_info_item3;
		private ImageView iv_card_info_item4;

		public ViewHolder() {
			super();
			// TODO Auto-generated constructor stub
			iv_card_info_item = new ImageView[4];
			iv_card_info_item[0] = iv_card_info_item1;
			iv_card_info_item[1] = iv_card_info_item2;
			iv_card_info_item[2] = iv_card_info_item3;
			iv_card_info_item[3] = iv_card_info_item4;
		}

	}

	private class CardOnClickListener implements OnClickListener {

		private int cardId;
		private ViewHolder holder;

		public CardOnClickListener(ViewHolder holder, int index) {
			// TODO Auto-generated constructor stub
			this.cardId = index;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mListener.showCardInfoListener(cardId, v);
		}

	}

}
